﻿// -----------------------------------------------------------------------
// <copyright file="CandidateCollection.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SoftServe.HrManager
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    
    using SoftServe.HrManager.Models;
    using SoftServe.HrManager.Abstractions;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class HrResources : CollectionBase, ICollection<Candidate>
    {
        #region ICollection<Candidate> implementation

        public void Add(Candidate item)
        {
            if (item == null)
                throw new ArgumentNullException();

            if (List.IndexOf(item) > -1)
                throw new InvalidOperationException("Candidate already exists");

            List.Add(item);
        }

        public bool Contains(Candidate item)
        {
            return List.IndexOf(item) > -1;
        }

        public void CopyTo(Candidate[] array, int arrayIndex)
        {
            List.CopyTo(array, arrayIndex);
        }

        public bool IsReadOnly
        {
            get { return List.IsReadOnly; }
        }

        public bool Remove(Candidate item)
        {
            int index = List.IndexOf(item);

            if (index > -1)
                List.Remove(item);

            return index > -1;
        }

        #endregion

        #region IEnumerable<Candidate> implementation

        IEnumerator<Candidate> IEnumerable<Candidate>.GetEnumerator()
        {
            foreach (Candidate candidate in List)
                yield return candidate;
        }

        #endregion

        public IEnumerable<Candidate> GetCandidatesBySkills(IEnumerable<eSkillName> skills)
        {
            foreach (Candidate candidate in List)
            {
                bool isMath = true;
                foreach (var skill in skills)
                {
                    if (mSkillChecker.AssertSkill(candidate, skill))
                    {
                        isMath = false;
                        break; 
                    }
                }

                if (isMath)
                    yield return candidate;
            }
        }

        private readonly SkillComparerBase mSkillChecker = new CandidateSkillChecker();
    }
}
